package com.symone.webcms.controller;

import com.symone.webcms.config.GlobalConfig;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.model.vo.FileVO;
import com.symone.webcms.service.impl.FileUploadService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(value="/file", description="文件管理")
@RequestMapping("api/file")
public class FileController {
    private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    @Autowired
    private FileUploadService uploadService;

    /**
     * @description : 获取所有文件记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-07
     */
    @RequiresPermissions("cms:file:index")
    @RequestMapping(value = "/aliyun/list", method = RequestMethod.GET)
    public ResultMessage list(String dir) {
        String bucketName = GlobalConfig.getInstance().get("sys_aliyun_oss_bucketname");
        List<FileVO> fileList = uploadService.getOSSList(bucketName, dir);
        return ResultMessage.success("获取所有文件信息", fileList);
    }

    @RequiresPermissions("cms:file:remove")
    @RequestMapping(value = "/local/remove", method = RequestMethod.DELETE)
    public ResultMessage localRemove(@RequestParam("key") String key,HttpServletRequest request){
        String url = "http://"+request.getServerName()+ ":"+ request.getServerPort()+"/";
        key = key.replace(url,"");
        if(uploadService.delete(key)){
            return ResultMessage.success("删除文件成功");
        }else{
            return ResultMessage.fail("删除文件失败");
        }
    }

    @RequiresPermissions("cms:file:remove")
    @RequestMapping(value = "/aliyun/remove", method = RequestMethod.DELETE)
    public ResultMessage aliyunRemove(@RequestParam("key") String key){
        key = key.replace(getAliyunOSSUrl(),"");
        if(uploadService.deleteOSS(key)){
            return ResultMessage.success("删除文件成功");
        }else{
            return ResultMessage.fail("删除文件失败");
        }
    }

    @RequestMapping(value = "/local/upload", method = RequestMethod.POST)
    public ResultMessage upload(@RequestParam("file") MultipartFile file,
                                Integer width,
                                Integer height,
                                HttpServletRequest request){
        logger.info("============>文件上传");
        try {
            if (!file.isEmpty()) {
                return uploadService.localUpload(file,request,width,height);
            }
        } catch (Exception ex) {
            return ResultMessage.fail(ex);
        }
        return ResultMessage.fail("文件上传失败!");
    }

    @RequestMapping(value = "/aliyun/upload", method = RequestMethod.POST)
    public ResultMessage aliyunUpload(@RequestParam("file") MultipartFile file) {
        logger.info("============>文件上传");
        try {
            if (null != file) {
                String uploadUrl = uploadService.aliyunUpload(file);
                return ResultMessage.success("文件上传成功!", getAliyunOSSUrl() +uploadUrl);
            }
        } catch (Exception ex) {
            return ResultMessage.fail(ex);
        }
        return ResultMessage.fail("文件上传失败!");
    }

    private String getAliyunOSSUrl(){
        String endpoint = GlobalConfig.getInstance().get("sys_aliyun_oss_endpoint");
        String bucketName = GlobalConfig.getInstance().get("sys_aliyun_oss_bucketname");
        return "http://" + bucketName + "." + endpoint +"/";
    }

}
